package com.xin.mmall.controller;


import com.xin.mmall.entity.Orders;
import com.xin.mmall.entity.User;
import com.xin.mmall.service.CartService;
import com.xin.mmall.service.OrderService;
import com.xin.mmall.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴玉鑫
 * @since 2021-05-13
 */
@Controller
@RequestMapping("/orders")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private CartService cartService;
    @PostMapping("/settlement3")
    public ModelAndView settlement3(Orders orders, HttpSession session,String address,String remark){
        ModelAndView modelAndView = new ModelAndView();
        User user = (User) session.getAttribute("user");
        orderService.save(orders,user,address,remark);
        modelAndView.addObject("cartList",cartService.findAllCartVOByUserId(user.getId()));
        modelAndView.setViewName("/settlement3");
        modelAndView.addObject("order",orders);
        return modelAndView;
    }
    @GetMapping("/list")
    public ModelAndView orderList(HttpSession session) {
        User user = (User) session.getAttribute("user");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("cartList",cartService.findAllCartVOByUserId(user.getId()));
        modelAndView.setViewName("orderList");
        List<OrderVO> orderVOList = orderService.ordersList(user);
        modelAndView.addObject("orderVOList",orderVOList);
        return modelAndView;
    }
}

